package ch.netcetera.planningpoker.client;

import java.util.logging.Logger;

import ch.netcetera.planningpoker.client.model.GameProxy;
import ch.netcetera.planningpoker.shared.GameRequest;
import ch.netcetera.planningpoker.shared.GameRequestFactory;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.ServerFailure;
import com.gwtplatform.mvp.client.DelayedBindRegistry;

public class IndexPage implements EntryPoint {

    private static final Logger logger = Logger.getLogger(IndexPage.class
            .getName());
	
    public final PokerGinjector ginjector = GWT.create(PokerGinjector.class);

    private GameRequestFactory requestFactory;

    /**
     * @see com.google.gwt.core.client.EntryPoint#onModuleLoad()
     */
    @Override
    public void onModuleLoad() {
        DelayedBindRegistry.bind(ginjector);

        ginjector.getPlaceManager().revealCurrentPlace();
        // final EventBus eventBus = new SimpleEventBus();
        // requestFactory = GWT.create(GameRequestFactory.class);
        // requestFactory.initialize(eventBus);
        //
        // // remove Loading-Message from page
        // RootPanel.getBodyElement().removeChild(
        // DOM.getElementById("Loading-Message"));
        //
        // FlowPanel panel = new FlowPanel();
        // final TextBox box = new TextBox();
        // panel.add(box);
        // Button createButton = new Button("Create");
        // panel.add(createButton);
        // createButton.addClickHandler(new ClickHandler() {
        //
        // @Override
        // public void onClick(ClickEvent event) {
        // String gameTitle = box.getText();
        // GameRequest gameRequest = requestFactory.getGameRequest();
        // GameProxy game = gameRequest.create(GameProxy.class);
        // game.setTitle(gameTitle);
        // gameRequest.persistGame(game).fire(new Receiver<Long>() {
        //
        // @Override
        // public void onSuccess(Long id) {
        // updateGame(id);
        // }
        //
        // });
        //
        // }
        // });
        // // create greetings message
        // RootPanel.get().add(panel);

    }

    private void updateGame(Long id) {

        logger.info("Update game with id " + id);

        GameRequest gameRequest2 = requestFactory.getGameRequest();
        gameRequest2.findGame(id).fire(new Receiver<GameProxy>() {

            @Override
            public void onFailure(ServerFailure error) {
                logger.severe("Call failed. " + error);
            }

            @Override
            public void onSuccess(GameProxy response) {
                logger.info("Success from find");
                persistGame(response);
            }
        });
    }

    private void persistGame(GameProxy game) {
        GameRequest gameRequest = requestFactory.getGameRequest();
        GameProxy edit = gameRequest.edit(game);
        edit.setTitle(edit.getTitle() + " bla");

        logger.info("Title of changed game: " + edit.getTitle());

        gameRequest.persistGame(edit).fire(new Receiver<Long>() {

            @Override
            public void onFailure(ServerFailure error) {
                logger.severe("Call failed. " + error);
            }

            @Override
            public void onSuccess(Long response) {
                logger.info("Changed game persisted with id: " + response);
            }

        });
    }

}
